package com.example.leetcode.trainingcamp.week12;

import java.util.ArrayList;
import java.util.List;

/**
 * 输入一个正整数 target ，输出所有和为 target 的连续正整数序列（至少含有两个数）。
 *
 * 序列内的数字由小到大排列，不同序列按照首个数字从小到大排列。
 *
 *  
 *
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode.cn/problems/he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
public class Offer57_II {

    public int[][] findContinuousSequence(int target) {

        int left = 1;
        int right = 2;
        int sum = left + right;
        List<int[]> list = new ArrayList<>();
        while (left < right) {
            if (sum == target) {
                int[] in = new int[right - left + 1];
                for (int i = left ;i<=right; i++) {
                    in[i - left] = i;
                }
                list.add(in);
                sum -= left;
                left++;
            }else if (sum < target) {
                right++;
                sum += right;
            }else {
                sum -= left;
                left++;
            }
        }
        return list.toArray(new int[list.size()][]);
    }

    public static void main(String[] args) {
        Offer57_II o = new Offer57_II();
        int[][] continuousSequence = o.findContinuousSequence(9);
        for (int[] i :continuousSequence) {
            System.out.println("\n");
            for (int num :i) {
                System.out.print(num);
            }
        }
    }
}
